#include <QDebug>
#include <QDir>
#include "Include/Project.h"
#include "Include/Settings.h"
#include "RopeProject.h"

Plugins::Project * __project = NULL;

namespace Plugins
{

Project::Project(const QString & projectPath)
{
    _set = new QSettings(projectPath, QSettings::IniFormat);
    __project = this;
    _rope = new RopeProject();
    _rope->initProject(path(), mainScript(), projectPath);
}

Project::~Project()
{
    delete _set;
    _set = NULL;
    delete _rope;
    _rope = NULL;
    __project = NULL;
    qDebug() << "rope deleted";
}

Project * Project::currentInstance()
{
    return __project;
}

QString Project::name()
{
    return _set->value("common/name", "").toString();
}

void Project::setName(const QString& name)
{
    _set->setValue("common/name", name);
}

int Project::pythonVersion()
{
    return _set->value("common/pyVersion", 3).toInt();
}

void Project::setPythonVersion(int version)
{
    _set->setValue("common/pyVersion", version);
}

QString Project::mainScript()
{
    return _set->value("common/mainscript", "").toString();
}

void Project::setMainScript(const QString& name)
{
    _set->setValue("common/mainscript", name);
}


QStringList Project::showFiles()
{
    return _set->value("common/showfiles", "*.py;*.ui;*.qrc;*.png;*.jpg;*.svg").toString().split(";");
}

void Project::setShowFiles(const QStringList & files)
{
    QString old = showFiles().join(";");
    _set->setValue("common/showfiles", files.join(";"));
    if (old != files.join(";"))
        emit needReloadTree();
}

QString Project::path()
{
    return QFileInfo(_set->fileName()).absoluteDir().absolutePath();
}

QString Project::filename()
{
    return _set->fileName();
}

QString Project::workspace()
{
    return path();
}

QStringList Project::openedDirs()
{
    QStringList list;
    int size = _set->beginReadArray("openedDirs");
    for (int i = 0; i < size; ++i) {
        _set->setArrayIndex(i);
        list.append(_set->value("dir").toString());
    }
    _set->endArray();
    return list;
}

QStringList Project::openedFiles()
{
    QStringList list;
    int size = _set->beginReadArray("openedFiles");
    for (int i = 0; i < size; ++i) {
        _set->setArrayIndex(i);
        list.append(_set->value("file").toString());
    }
    _set->endArray();
    return list;
}

void Project::openDir(const QString& dir)
{
    QStringList dirs = openedDirs();
    if (!dirs.contains(dir)){
        dirs.append(dir);
        saveList(dirs, "openedDirs", "dir");
    }
}

void Project::closeDir(const QString& dir)
{
    QStringList dirs = openedDirs();
    if (dirs.contains(dir))
        dirs.removeOne(dir);
    saveList(dirs, "openedDirs", "dir");
}

void Project::openFile(const QString& file)
{
    QStringList files = openedFiles();
    files.append(file);
    saveList(files, "openedFiles", "file");
}

void Project::closeFile(const QString& file)
{
    QStringList files = openedFiles();
    if (files.contains(file)){
        files.removeAll(file);
    }
    saveList(files, "openedFiles", "file");
}

void Project::saveList(const QStringList& items, const QString& key, const QString& lkey)
{
    _set->beginWriteArray(key);
    QStringList saveItems = items;
    saveItems.removeDuplicates();
    for(int i = 0; i < saveItems.length(); ++i){
        _set->setArrayIndex(i);
        _set->setValue(lkey, items[i]);
    }
    _set->endArray();
}

IRopeProject * Project::rope() const
{
    return _rope;
}

QVariant Project::lintConfig(const QString & name, const QVariant & defVal)
{
    return _set->value(QString("lint/%1").arg(name), defVal);
}

bool Project::setLintConfig(const QString & name, const QVariant & value)
{
    QVariant old = _set->value(QString("lint/%1").arg(name));
    _set->setValue(QString("lint/%1").arg(name), value);
    return old != value;
}

int Project::pylintPreset(int defVal)
{
    return _set->value("lint/preset", defVal).toInt();
}

void Project::setPylintPreset(int value)
{
    _set->setValue("lint/preset", value);
}

QString Project::python()
{
    switch(pythonVersion())
    {
    case 2:
        return Settings::instance()->py2();
    default:
        return Settings::instance()->py3();
    }
}

void Project::save()
{
}

QString Project::lastSearch()
{
    return _set->value("findinproject/last-search", "").toString();
}

QString Project::lastSearchFilePattern()
{
    return _set->value("findinproject/file-pattern", "*").toString();
}

int Project::searchFlags()
{
    return _set->value("findinproject/search-flags", 0).toInt();
}

void Project::setLastSearch(const QString & search)
{
    _set->setValue("findinproject/last-search", search);
}

void Project::setLastSearchFilePattern(const QString & patt)
{
    _set->setValue("findinproject/file-pattern", patt);
}

void Project::setSearchFlags(int flags)
{
    _set->setValue("findinproject/search-flags", flags);
}

QString Project::guiVersion()
{
    return _set->value("common/guiVersion", "").toString();
}

void Project::setGuiVersion(const QString& version)
{
    _set->setValue("common/guiVersion", version);
}

}


